<template>
  <div class="base-container" id="main">
    <t-form ref="form" :data="queryParams" label-width="80" @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}" @reset="()=>{this.queryParams.pageNum=1;this.queryParams.pageNum=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="标准名称" name="name">
            <t-input
              v-model="queryParams.name"
              clearable
              placeholder="请输入标准名称"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="标准状态" name="localStandardStatus ">
            <t-select clearable  v-model="queryParams.localStandardStatus"
                      :options="dict.type.local_standard_status">
            </t-select>
          </t-form-item>
          <t-form-item label="标准编号" name="projectNum">
            <t-input
              v-model="queryParams.projectNum"
              clearable
              placeholder="请输入标准编号"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="起草单位" name="draftUnitNames">
            <t-input
              v-model="queryParams.draftUnitNames"
              clearable
              placeholder="请输入起草单位"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="起草人员" name="draftPersonNames">
            <t-input
              v-model="queryParams.draftPersonNames"
              clearable
              placeholder="请输入起草单位"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="发布时间" name="publicDateRange">
            <t-date-range-picker style="width: 280px" :placeholder="['开始时间','结束时间']" v-model="queryParams.publicDateRange"  allow-input clearable/>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
     <!-- v-if="this.queryParams.standardCategory" -->
    <div class="btn-group-inline">
      <!-- <t-button @click="oldApply" variant="outline" theme="primary">
        <t-icon name="add" slot="icon"/>
        历史登记
      </t-button> -->
      <!-- v-if="!['1','2','4','3','5','6'].includes(queryParams.standardCategory)" -->
      <t-button  @click="apply" variant="outline" theme="primary">
       <t-icon name="add" slot="icon"/>
       登记
     </t-button>
    </div>
    <t-table rowKey="index" size="small" :data="data" :columns="columns" hover :pagination="pagination"
             :loading="dataLoading" :sort="sort" :hideSortTips="true" @sort-change="sortChange" multipleSort >
      <template #name="{ row }">
        <t-link v-if="row.isOldProject === '1'" @click="dialog.header=row.name;dialog.objId=row.localId;dialog.open_standard_detail=true" hover="color" theme="primary">{{ row.name }}</t-link>
        <t-link v-else @click="dialog.header=row.name;dialog.objId=row.localId;dialog.open_register_detail=true" hover="color" theme="primary">{{ row.name }}</t-link>
      </template>
      <template #formulateType="{ row }">
        {{ row.formulateType | filterByDict(dict.type.formulate_type) }}
      </template>
      <template #systemName="{ row }">
        {{row.systemName || "-"}}
      </template>
      <template #standardProperty="{ row }">
        {{ row.standardProperty | filterByDict(dict.type.standard_property) }}
      </template>
      <template #standardCategory="{ row }">
        {{ row.standardCategory | filterByDict(dict.type.standard_category) }}
      </template>
      <template #reviseNum="{ row }">
        {{row.reviseNum || "-"}}
      </template>
      <template #consistencyDegree="{ row }">
        {{row.consistencyDegree || "-"}}
      </template>
      <template #completeDate="{ row }">
        {{row. completeDate|| "-"}}
      </template>
      <template #publishDate="{ row }">
        {{row. publishDate|| "-"}}
      </template>
      <template #implDate="{ row }">
        {{row. implDate|| "-"}}
      </template>
      <template #internationalStandardNum="{ row }">
        {{row.internationalStandardNum || "-"}}
      </template>
      <template #signStatus="{ row }">
        <t-tag  @click="dialog.header=row.name;dialog.objId=row.localId;dialog.standardCategory=row.standardCategory;dialog.open_approve_list=true" class="cursor-pointer" :theme="handleTheme(row.signStatus, dict.type.local_standard_sign_status)" variant="light">
          {{ row.signStatus | filterByDict(dict.type.local_standard_sign_status) }}
        </t-tag>
      </template>
      <template #manageDeptId="{ row }">
        {{ row.manageDeptId | filterByTree(deptMenuOptions) }}
      </template>
      <template #op="{ row }">
         <t-button  v-if="row.isOldProject === '1'" size="small" @click="dialog.header=row.name;dialog.objId=row.localId;dialog.open_standard_detail=true" variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon"/>
          详情
        </t-button>
        <t-button  v-if="row.isOldProject !== '1'" size="small" @click="dialog.header=row.name;dialog.objId=row.localId;dialog.open_register_detail=true" variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon"/>
          详情
        </t-button>
        <t-button
          v-if="row.isOldProject === '0' && row.signStatus === '1'"
          @click="update(row)"
          size="small"
          variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          继续登记
        </t-button>
        <t-button
          v-if="['1','4',null].includes(row.signStatus)"
          theme="danger"
          @click="delObj(row)"
          size="small"
          variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon"/>
          删除
        </t-button>
      </template>
    </t-table>
    <approveList :header="dialog.header" :obj-id="dialog.objId" :standardCategory="dialog.standardCategory"  :visible.sync="dialog.open_approve_list" v-if="dialog.open_approve_list"></approveList>
    <register-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_register_detail" v-if="dialog.open_register_detail"></register-detail-dialog>
    <standard-detail-dialog-old :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_standard_detail" v-if="dialog.open_standard_detail"></standard-detail-dialog-old>
  </div>
</template>

<script>
// import standardDetailDialog from "../standard/components/standard-detail-dialog-old.vue";
import FileUpload from "@/components/FileUpload/index.vue"
import approveList from "@/pages/bzh/approve/components/approve-list.vue";
import registerDetailDialog from "./components/register-detail-dialog.vue";
import standardDetailDialogOld from  "@/pages/bzh/standard/components/standard-detail-dialog-old.vue";

export default {
  name: "standard-my",
  dicts:["formulate_type","bzh_approve_type","approve_status","local_standard_sign_status","standard_property","standard_category"],
  components: {
    approveList,
    FileUpload,
    registerDetailDialog,
    standardDetailDialogOld
  },
  data() {
    return {
      fileList:[],
      fileIds:"",
      rules:{
        name: [
          {
            required: true,
            message: "立项名称不能为空",
          },
        ],
        projectNum: [
          {
            required: true,
            message: "标准编号不能为空",
          },
        ],
        fileIds: [
          {
            required: true,
            message: "相关文件不能为空",
          },
        ]},
      deliverDialog:false,
      dialog: {
        objId: null,
        header:"",
        open_detail: false,
        open_approve_list: false,
        open_register_detail: false,
        open_standard_detail:false,
        standardCategory: '',
      },
      nowRow:{},
      deleteVisible:false,
      deptMenuOptions:[],
      queryParams:{
        pageNum:1,
        pageSize:10,
        name:"",
        formulateType:"",
        publicDateRange: [],
        standardCategory:"",
        startPublishDate: '',
        endPublishDate: '',
      },
      formData:{
        name:"",
        projectNum:"",
        fileIds:""
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
      sort:[],
      dataLoading: false,
      columns:[
        {
          colKey: "name",
          title: "标准名称",
          align: "center",
          width: 220
        },
        {
          colKey: "systemName",
          title: "标准体系",
          align: "center",
          width: 160,
        },
        {
          colKey: "projectNum",
          title: "标准编号",
          align: "center",
          width: 160,
        },
        {
          colKey: "standardProperty",
          title: "标准性质",
          align: "center",
          width: 160,
        },
        {
          colKey: "standardCategory",
          title: "标准类别",
          align: "center",
          width: 160,
        },
        {
          colKey: "formulateType",
          title: "制定或修订",
          align: "center",
          width: 160,
        },
        {
          colKey: "draftUnitNames",
          title: "主要起草单位",
          align: "center",
          width: 160,
          ellipsis: true,
        },
        {
          colKey: "completeDate",
          title: "提出时间",
          align: "center",
          width: 160,
          sorter: true,
        },
        {
          colKey: "publishDate",
          title: "发布时间",
          align: "center",
          width: 160,
          sorter: true,
        },
        {
          colKey: "implDate",
          title: "实施时间",
          align: "center",
          width: 160,
          sorter: true,
        },
        {
          colKey: "signStatus",
          title: "登记状态",
          align: "center",
          width: 160,
          fixed: 'right'
        },
        // {
        //   colKey: "approveStatus",
        //   title: "审批状态",
        //   align: "center",
        //   width: 160,
        // },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 240,
          fixed: 'right'
        },
      ],
      data: []
    }
  },
  async created() {
    if(this.$route.meta.query){
      Object.assign(this.queryParams, JSON.parse(this.$route.meta.query))
    }
    this.getList()
    this.getDeptTreeSelect()
  },
  methods: {
    // 排序
    sortChange(sort){
      this.sort = sort;
      let orderByColumn = []
      let isAsc = []
      if(sort instanceof Array){
        orderByColumn = sort.map(val=>val.sortBy)
        isAsc = sort.map(val=>val.descending?`desc`:`asc`)
      }else {
        orderByColumn = [sort.sortBy]
        isAsc = [sort.descending?`desc`:`asc`]
      }
      this.queryParams.orderByColumn = orderByColumn.join(",")
      this.queryParams.isAsc = isAsc.join(",")
      this.getList();
    },
    // 获取接口数据
    getList(){
      [this.queryParams.startPublishDate,this.queryParams.endPublishDate] = this.queryParams.publicDateRange;
      this.dataLoading = true
      return this.$api.bzh.register.myList(this.queryParams).then(response=>{
        this.pagination.total = response.total
        this.data = response.rows
        this.dataLoading = false
      }).catch(e=>{
        this.dataLoading = false
        this.$message.error(e.toString())
      })
    },
    getDeptTreeSelect(){
      return this.$api.system.dept.treeSelectAllIn().then(response=>{
        this.deptMenuOptions = response.data
      }).catch(e=>{
        this.$message.error(e.toString())
      })
    },
    // 编辑按钮
    update(row){
      const title = row.isOldProject==='1'? '历史登记' : '登记'
      this.$router.push({
        path:`/bzh/other/register/register-apply/${row.id}`,
        query:{
          standardCategory: row.standardCategory,
          title,
          isOldProject: row.isOldProject,
        }
      });
    },
    apply(){
      // const title = this.getTitle(this.queryParams.standardCategory, '')
      const title = "登记"
      this.$router.push({
        path:`/bzh/other/register/register-apply/${Date.parse(new Date())}`,
        query:{
          // standardCategory: this.queryParams.standardCategory,
          title,
          isOldProject: '0',
        }
      })
    },
    oldApply(){
      // const title = `${this.getTitle(this.queryParams.standardCategory, '')}(历史)`
      const title = "历史登记"
      this.$router.push({
        path:`/bzh/other/register/register-apply/${Date.parse(new Date())}`,
        query:{
          // standardCategory: this.queryParams.standardCategory,
          title,
          isOldProject: '1',
        }
      })
    },
    getTitle(standardCategory, projectName){
      const titleDict =  {
        "1": "国家标准登记",
        "2": "行业标准登记",
        "3": "地方标准登记",
        "4": "团体标准登记",
        "5": "企业标准登记",
        "6": "厅技术指南登记",
      }
      return projectName? `${projectName}-${titleDict[standardCategory]}`:titleDict[standardCategory]
    },
    delObj(row){
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除标准名称为"${row.name}"的数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.bzh.register.delete(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        }
      });
    },
  }
}
</script>

<style lang="less" scoped>

</style>

